<template>
  <div class="app-container">

		<div class="filter-container">
			<el-input  style="width: 200px;"   v-model="query.realName" placeholder="真实姓名" class="filter-item"  ></el-input>
			<el-input  style="width: 200px;"   v-model="query.account" placeholder="用户名" class="filter-item"  ></el-input>
			<el-input  style="width: 200px;"   v-model="query.phone" placeholder="电话" class="filter-item"  ></el-input>

			<!--
			<el-select v-model="type" class="filter-item"  placeholder="请选择">
					<el-option v-for="item in mapType" :key="item.value" :label="item.label" :value="item.value"></el-option>
			</el-select>

			 <el-date-picker class="filter-item"
        v-model="query.startDate"
        type="date"
        placeholder=""
        value-format="yyyy-MM-dd"
      >
      </el-date-picker>

      <el-date-picker class="filter-item"
        v-model="query.createdAt" value-format="yyyy-MM-dd HH:mm:ss"
        type="datetimerange"
        range-separator="至"
        start-placeholder="录入开始日期"
        end-placeholder="录入结束日期">
      </el-date-picker>


			-->

			<el-button @click="getRecords" type="primary" class="filter-item">查询</el-button>

      <el-button @click="handleDelete()" type="primary" class="filter-item">删除</el-button>

      <el-button @click="download" type="primary" class="filter-item">导出</el-button>

      <div class="filter-item">
        <el-upload
          style="margin-left: 10px"
          :action="host+'/guard/importFile?importType=user&token='+token"
          accept=".xls,.xlsx"
          :on-success="uploadSuccess"
          :on-error="uploadError"
          :on-progress="uploadProcess"
          :show-file-list="false">
          <el-button type="primary">导入</el-button>
        </el-upload>
      </div>
      <el-button style="margin-left: 10px" @click="getRecords" type="danger" class="filter-item">刷新</el-button>
		</div>

		<el-table v-loading="listLoading"	:data="list"	element-loading-text="Loading"	border	fit	highlight-current-row
              @selection-change="handleSelectionChange"
              v-horizontal-scroll="'always'">
			 <el-table-column type="selection" width="55" />
			<!-- <el-table-column type="index" label="序列" width="50" /> -->
			<el-table-column fixed="left" prop="id" label="ID" min-width="50" />
			<el-table-column prop="avatar" label="头像" min-width="80" >
					<template slot-scope="scope">
						<img v-if="scope.row.avatar" class="table-img" :src="host+scope.row.avatar">
					</template>
			</el-table-column>
			<el-table-column prop="account" label="用户名" min-width="120" />
			<el-table-column prop="real_name" label="真实姓名" min-width="120" />
			<el-table-column prop="phone" label="电话" min-width="120" />
			<el-table-column prop="age" label="年龄" min-width="80" />
			<el-table-column prop="gender" label="性别" min-width="80" >
				<template slot-scope="scope">
						<el-tag v-if="scope.row.gender==1">男</el-tag>
						<el-tag v-else type="danger">女</el-tag>
				</template>
			</el-table-column>
			<el-table-column prop="address" label="地址" min-width="150" />
			<el-table-column show-overflow-tooltip prop="beizhu" label="备注" min-width="300" />
			<el-table-column prop="created_at" label="创建时间" min-width="160" />
			<el-table-column prop="status" label="禁用/启用" min-width="100" >
				<template slot-scope="scope">
						<el-switch	v-model="scope.row.status"	active-color="#409EFF"	inactive-color="#909399"
							:active-value="1"	:inactive-value="2"		@change="changeStatus(scope.row)">
						</el-switch>
				</template>
		</el-table-column>
			<el-table-column fixed="right" label="操作" min-width="100">
				<template slot-scope="scope">
					<el-button size="mini" type="text" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
<!--					<el-button size="mini" type="text" @click="handleDelete(scope.$index, scope.row)">删除</el-button>-->
				</template>
			</el-table-column>
		</el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagination.page"
      :page-sizes="[10, 20, 50, 100, 200]"
      :page-size="pagination.pageSize"
      :background="true"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pagination.total">
    </el-pagination>


  </div>
</template>

<script>

import { myGet,myPost,exportXls } from '@/utils/myUtils'
import {getToken} from "@/utils/auth";

const urlUserLst = '/guard/user/lst';
const urlAddUser = '/guard/user/add';
const urlGetUser = '/guard/user/detail';
const urlDeleteUser = '/guard/user/del';
const urlDownloadUser = '/guard/download';
export default {

    data() {
        return {
          token:"",
					host:process.env.VUE_APP_HOST,
					isAdd:true,
					addDialogFormVisible:false,
					mapType:[],
          selectId:[],
          query:{
						account:'',
						realName:'',
						phone:'',
					},
					pagination:{
						total:0,
						page:1,
						pageSize:50,
					},
					dialogFormVisible:false,
					list: [],
					formLoading:false,
					listLoading: true
        }
    },
    methods: {

      uploadSuccess(res, file, fileList){
        if(res.status){
           this.$message.success(res.msg)
           this.getRecords();
        }else{
          this.$message.error(res.msg)
        }
      },
      uploadError(err, file, fileList){
        //console.log(err)
      },
      uploadProcess(event, file, fileList){
        //console.log(event)
      },

      download(){
        const url = this.host+urlDownloadUser+"?downloadType=user"
        exportXls(url);
      },

      reload(){
        Object.assign(this.$data.query,this.$options.data().query)
        Object.assign(this.$data.pagination,this.$options.data().pagination)
        this.getRecords()
      },
			getRecords(){
        this.listLoading = true;
        let params = Object.assign(this.query,this.pagination)
					myGet(urlUserLst,params).then((res) =>{
            this.listLoading = false;
            if(res.status){
              this.list = res.data.records
              this.pagination.total = res.data.total
            }
          })
			},
			changeStatus(row){
        myPost(urlAddUser,{id:row.id, status:row.status}).then(res => {
          if(res.status){
            this.$message.success(res.msg)
          }
        })
			},
			handleEdit(index, row) {
				const id = row.id
				this.$router.push({path:`edit/${id}`})
			},

      handleDelete(){
        if(this.selectId.length<=0){
          this.$message.error('请先选择项目！')
          return;
        }
        this.$confirm('确定要删除吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          const id = this.selectId
          myGet(urlDeleteUser,{id}).then(res=>{
            if(res.status){
              this.$message.success(res.msg)
              this.getRecords()
            }
          })
        });
      },

      handleSelectionChange(val) {
        this.selectId = [];
        val.forEach(item=>{
          this.selectId.push(item.id)
        })
      },

			handleSizeChange(val) {
					this.pagination.pageSize = val
					this.pagination.page = 1
					this.getRecords()
			},
			handleCurrentChange(val) {
					this.pagination.page = val
					this.getRecords();
			},
			//多选
			toggleSelection(rows) {
					if (rows) {
					rows.forEach(row => {
							this.$refs.multipleTable.toggleRowSelection(row);
					});
					} else {
							this.$refs.multipleTable.clearSelection();
					}
			}
    },
    mounted() {
      this.getRecords()
      this.token = getToken()

      //console.log(this.host)
    }
}
</script>

<style>

.table-img{
    width: 50px;
    height: 50px;
}

.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}
.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}
.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
}
.avatar {
    width: 100px;
    height: 100px;
    display: block;
}
</style>


